import axios from 'axios';
import {getToken} from "./auth"
import {Message} from "element-ui"
// 创建axios实例
const service = axios.create({
	baseURL: process.env.VUE_APP_BASE_API, // api 的 base_url
	timeout: 10000 // 请求超时时间
});

// request拦截器
service.interceptors.request.use(
	config => {
		if (getToken()) {
			config.headers['Authorization'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
		}
		config.headers['Content-Type'] = 'application/json'
		return config
	},
	error => {
		return Promise.reject(error)
	}
)

// response 拦截器
service.interceptors.response.use(
	response => {
		return response.data
	},
	error => {
		const message = error.response.data.message
		const status = error.response.data.status
		Message({
			message,
			duration: 2000
		})
		// console.log(error.response.data)
		switch (status) {
			case 400:
				break
			case 401:
				break
			case 403:
				break
			case 404:
				break

		}
		return Promise.reject(error)
	}
)

export default service;
